﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TencentCloud.Taf.V20200210.Models;

namespace MDT.LifeSewagePlatform.Conmmon
{
    public static class StringExtension
    {
        public static string GetFileName(this string filePath, int index = -1)
        {
            string name = string.Empty; ;
            if (string.IsNullOrEmpty(filePath))
                return null;

            if (filePath.Contains("\\"))
            {
                string[] strs = filePath.Split("\\");
                name = strs[strs.Length - 1];
            }

            return name;
        }
        public static string GetFileDic(this string filePath, int index = -1)
        {
            string name = string.Empty; ;
            if (string.IsNullOrEmpty(filePath))
                return null;

            if (filePath.Contains("\\"))
            {
                string[] strs = filePath.Split("\\");
                name = strs[strs.Length - 1];
            }

            return name;
        }

        public static string StrRemove(this string str, bool IsEnd, int length)
        {
            if (string.IsNullOrEmpty(str))
            {
                return string.Empty;
            }

            if (IsEnd)
            {
                return str.Remove(str.Length - length, length);
            }
            else
            {
                return str.Remove(0, length);
            }
        }
        public static int[] ConvertToIntArry(this string str, string partStr)
        {
            if (string.IsNullOrEmpty(partStr) == false)
            {
                if (string.IsNullOrEmpty(str))
                {
                    return str.Split(partStr).Select(s =>
                    {
                        int.TryParse(s, out int val);
                        return val;
                    }).ToArray();
                }
            }
            return new int[0];
        }

        public static string GetToken(this string BearerToken)
        {
            if (BearerToken.Contains(" "))
            {
                string[] str = BearerToken.Split(' ');
                if (str.Length > 1)
                {
                    return str[1];
                }
            }
            return BearerToken;
        }

        /// <summary>
        /// 将ip从string类型转换为int类型
        /// </summary>
        /// <param name="ip"></param>
        /// <returns></returns>
        public static long IpToInt(this string ip)
        {
            if (ip.Contains("."))
            {
                char[] separator = new char[] { '.' };
                string[] items = ip.Split(separator);
                if (items.Length>=4)
                {
                return items[0].ConvertToLong()<< 24
                        | items[1].ConvertToLong() << 16
                        |items[2].ConvertToLong() << 8
                        | items[3].ConvertToLong();
                }
                
            }
            return 0;
        }

        public static long ConvertToLong(this string str)
        {
            long.TryParse(str,out long res);
            return res;
        }
        public static string CutOut(this string str, string startStr, string endStr)
        {
            if (str.Contains(startStr) == false && str.Contains(endStr) == false)
            {
                return string.Empty;
            }
            int start = str.IndexOf(startStr) + 2;
            int length = str.IndexOf(endStr) - start;
            return str.Substring(start, length);
        }
    }
}
